<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="ID" prop="portId">
        <el-input v-model="queryParams.portId" placeholder="请输入ID" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="设备ID" prop="deviceId">
        <el-input v-model="queryParams.deviceId" placeholder="请输入设备ID" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['business:ports:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['business:ports:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['business:ports:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="ID" align="center" prop="portId" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="端口描述类型" align="center" prop="portDescrType" />
      <el-table-column label="端口描述详情" align="center" prop="portDescrDescr" />
      <el-table-column label="端口描述电路信息" align="center" prop="portDescrCircuit" />
      <el-table-column label="端口描述速度" align="center" prop="portDescrSpeed" />
      <el-table-column label="端口描述备注" align="center" prop="portDescrNotes" />
      <el-table-column label="接口描述" align="center" prop="ifDescr" />
      <el-table-column label="网络接口名称" align="center" prop="ifName" />
      <el-table-column label="端口名称" align="center" prop="portName" />
      <el-table-column label="接口索引" align="center" prop="ifIndex" />
      <el-table-column label="接口速度" align="center" prop="ifSpeed" />
      <el-table-column label="上一次接口速度" align="center" prop="ifspeedPrev" />
      <el-table-column label="接口连接器是否存在" align="center" prop="ifConnectorPresent" />
      <el-table-column label="接口操作状态" align="center" prop="ifOperStatus" />
      <el-table-column label="接口上一次操作状态" align="center" prop="ifoperstatusPrev" />
      <el-table-column label="接口管理状态" align="center" prop="ifAdminStatus" />
      <el-table-column label="接口上一次管理状态" align="center" prop="ifadminstatusPrev" />
      <el-table-column label="接口双工模式" align="center" prop="ifDuplex" />
      <el-table-column label="接口最大传输单元" align="center" prop="ifMtu" />
      <el-table-column label="接口类型" align="center" prop="ifType" />
      <el-table-column label="接口别名" align="center" prop="ifAlias" />
      <el-table-column label="接口物理地址" align="center" prop="ifPhysAddress" />
      <el-table-column label="接口最后更改时间" align="center" prop="ifLastChange" />
      <el-table-column label="接口 VLAN 信息" align="center" prop="ifVlan" />
      <el-table-column label="接口链路聚合信息" align="center" prop="ifTrunk" />
      <el-table-column label="接口虚拟路由转发信息" align="center" prop="ifVrf" />
      <el-table-column label="是否忽略标识" align="center" prop="ignore" />
      <el-table-column label="是否禁用标识" align="center" prop="disabled" />
      <el-table-column label="PAgP 操作模式" align="center" prop="pagpOperationMode" />
      <el-table-column label="PAgP 端口状态" align="center" prop="pagpPortState" />
      <el-table-column label="PAgP 对端设备 ID" align="center" prop="pagpPartnerDeviceId" />
      <el-table-column label="PAgP 对端学习方法" align="center" prop="pagpPartnerLearnMethod" />
      <el-table-column label="PAgP 对端接口索引" align="center" prop="pagpPartnerIfIndex" />
      <el-table-column label="PAgP 对端组接口索引" align="center" prop="pagpPartnerGroupIfIndex" />
      <el-table-column label="PAgP 对端设备名称" align="center" prop="pagpPartnerDeviceName" />
      <el-table-column label="PAgP 以太网通道操作模式" align="center" prop="pagpEthcOperationMode" />
      <el-table-column label="PAgP 设备 ID" align="center" prop="pagpDeviceId" />
      <el-table-column label="PAgP 组接口索引" align="center" prop="pagpGroupIfIndex" />
      <el-table-column label="入站单播数据包数量" align="center" prop="ifInUcastPkts" />
      <el-table-column label="上一次入站单播数据包数量" align="center" prop="ifinucastpktsPrev" />
      <el-table-column label="入站单播数据包数量差值" align="center" prop="ifinucastpktsDelta" />
      <el-table-column label="入站单播数据包速率" align="center" prop="ifinucastpktsRate" />
      <el-table-column label="出站单播数据包数量" align="center" prop="ifOutUcastPkts" />
      <el-table-column label="上一次出站单播数据包数量" align="center" prop="ifoutucastpktsPrev" />
      <el-table-column label="出站单播数据包数量差值" align="center" prop="ifoutucastpktsDelta" />
      <el-table-column label="出站单播数据包速率" align="center" prop="ifoutucastpktsRate" />
      <el-table-column label="入站错误数据包数量" align="center" prop="ifInErrors" />
      <el-table-column label="上一次入站错误数据包数量" align="center" prop="ifinerrorsPrev" />
      <el-table-column label="入站错误数据包数量差值" align="center" prop="ifinerrorsDelta" />
      <el-table-column label="入站错误数据包速率（单位：包/秒）" align="center" prop="ifinerrorsRate" />
      <el-table-column label="出站错误数据包数量。" align="center" prop="ifOutErrors" />
      <el-table-column label="上一次出站错误数据包数量。" align="center" prop="ifouterrorsPrev" />
      <el-table-column label="出站错误数据包数量差值。" align="center" prop="ifouterrorsDelta" />
      <el-table-column label="出站错误数据包速率（单位：包/秒）。" align="center" prop="ifouterrorsRate" />
      <el-table-column label="入站字节数（流量统计）" align="center" prop="ifInOctets" />
      <el-table-column label="上一次入站字节数。" align="center" prop="ifinoctetsPrev" />
      <el-table-column label="入站字节数差值。" align="center" prop="ifinoctetsDelta" />
      <el-table-column label="入站流量速率（单位：字节/秒）。" align="center" prop="ifinoctetsRate" />
      <el-table-column label="出站字节数。" align="center" prop="ifOutOctets" />
      <el-table-column label="上一次出站字节数。" align="center" prop="ifoutoctetsPrev" />
      <el-table-column label="出站字节数差值。" align="center" prop="ifoutoctetsDelta" />
      <el-table-column label="出站流量速率（单位：字节/秒）。" align="center" prop="ifoutoctetsRate" />
      <el-table-column label="最后一次轮询时间（时间戳）。" align="center" prop="pollTime" />
      <el-table-column label="上一次轮询时间。" align="center" prop="pollPrev" />
      <el-table-column label="轮询间隔时间（秒）。" align="center" prop="pollPeriod" />
      <el-table-column label="是否是关联链路0否1是" align="center" prop="isCritical" />
      <el-table-column label="是否展示0否1是" align="center" prop="isShow" />
      <el-table-column label="实际上行带宽" align="center" prop="bandwidth" />
      <el-table-column label="入站丢包数量" align="center" prop="ifInLoss" />
      <el-table-column label="出站丢包数量" align="center" prop="ifOutLoss" />
      <el-table-column label="接口非单播包转发数（in）" align="center" prop="ifInNoUnicastForward" />
      <el-table-column label="接口非单播包转发数（out）" align="center" prop="ifOutNoUnicastForward" />
      <el-table-column label="丢弃的未知协议包" align="center" prop="ifUnknownDrop" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:ports:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['business:ports:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PortsForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import download from '@/utils/download'
import { PortsApi, Ports } from '@/api/business/ports'
import PortsForm from './PortsForm.vue'

/** 端口 列表 */
defineOptions({ name: 'Ports' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<Ports[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  portId: undefined,
  deviceId: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PortsApi.getPortsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PortsApi.deletePorts(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除端口 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await PortsApi.deletePortsList(checkedIds.value);
    checkedIds.value = [];
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: Ports[]) => {
  checkedIds.value = records.map((item) => item.id!);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PortsApi.exportPorts(queryParams)
    download.excel(data, '端口.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>